﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using System.IO;
using System.Drawing;

namespace PPWMS.View
{
    class EncrpytDecrpyt
    {

        public EncrpytDecrpyt() 
        {
            CheckUSBKeyInfo();
        }

        #region   对数据进行解密

        private string encryptkey = "HBSciTech";    //密钥  


        private void CheckUSBKeyInfo()
        {
            ////初始化我们的操作加密锁的类
            //SoftKeyYT88 ytsoftkey = new SoftKeyYT88();
            ////这个用于判断系统中是否存在着加密锁。不需要是指定的加密锁,
            //if (ytsoftkey.FindPort(0, ref _keyPath) != 0)
            //{
            //    MessageBox.Show("未找到加密锁,请插入加密锁后，再进行操作。");
            //    return;
            //}


            ////从加密锁中读取字符串,使用默认的读密码
            //String outString = "";
            //short myLen = 70;//注意这里的6是长度，要与写入的字符串的长度相同
            //if (ytsoftkey.YReadString(ref outString, 0, myLen, "ffffffff", "ffffffff", _keyPath) != 0)
            //{
            //    MessageBox.Show("读字符串失败");
            //    return;
            //}
            //if (outString != "上海慧标信息科技有限公司")
            //{
            //    MessageBox.Show("加密锁信息有误，请更换正确的加密锁！");
            //    return;
            //}
        }

        /// <summary>
        /// 对数据进行解密
        /// </summary>
        /// <param name="decryptstring">需要解密的数据</param>
        /// <returns></returns>
        public string DESDecrypt(string decryptstring)
        {
            string strRtn;
            try
            {
                DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
                byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey);
                byte[] data = Convert.FromBase64String(decryptstring);
                MemoryStream ms = new MemoryStream();//存储解密后的数据
                CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(key, key), CryptoStreamMode.Write);
                cs.Write(data, 0, data.Length);//解密数据
                cs.FlushFinalBlock();
                strRtn = System.Text.Encoding.Unicode.GetString(ms.ToArray());
                return strRtn;
            }
            catch //(Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region   对数据进行加密
        /// <summary>
        /// 对数据进行加密
        /// </summary>
        /// <param name="encryptstring">需要加密的数据</param>
        /// <returns></returns>
        public string DESEncrypt(string encryptstring)
        {
            string strRtn;
            try
            {
                DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密
                byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey);
                byte[] data = System.Text.Encoding.Unicode.GetBytes(encryptstring);
                MemoryStream ms = new MemoryStream();//存储加密后的数据
                CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
                cs.Write(data, 0, data.Length);//进行加密
                cs.FlushFinalBlock();
                strRtn = Convert.ToBase64String(ms.ToArray());
                return strRtn;
            }

            catch //(Exception ex)
            {
                return null;
            }
        }
        #endregion
    }
}
